package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    SetmealService setmealService;

    @Autowired
    SetmealDishService setmealDishService;

    @Autowired
    CategoryService categoryService;

    @PostMapping
    public R<String> saveSetmealWithDish(@RequestBody SetmealDto setmealDto){
        log.info(setmealDto.toString());
        setmealService.saveSetmealWithDish(setmealDto);
        return R.success("添加成功");
    }

    @GetMapping("/page")
    public R<Page> findPage(Integer page,Integer pageSize,String name){

        //分页构造器
        Page<Setmeal> setmealPage=new Page<>(page,pageSize);
        //条件构造器
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper=new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.like(name!=null,Setmeal::getName,name);
        //排序构造器
        setmealLambdaQueryWrapper.orderByAsc(Setmeal::getUpdateTime).orderByAsc(Setmeal::getStatus);
        setmealService.page(setmealPage,setmealLambdaQueryWrapper);

        Page<SetmealDto> setmealDtoPage=new Page<>();

        //将setmealPage 中的内容复制到setmealDtoPage中  records属性 不复制
        BeanUtils.copyProperties(setmealPage,setmealDtoPage,"records");
        List<Setmeal> records = setmealPage.getRecords();

        List<SetmealDto> setmealDtoList = records.stream().map(new Function<Setmeal, SetmealDto>() {
            @Override
            public SetmealDto apply(Setmeal setmeal) {
                //将每个setmeal复制到setmealDto中
                SetmealDto setmealDto = new SetmealDto();
                BeanUtils.copyProperties(setmeal, setmealDto);
                //获取categoryId
                Long categoryId = setmeal.getCategoryId();
                //根据categoryId 去分类表中查找 分类名称
                Category category = categoryService.getById(categoryId);
                String categoryName = category.getName();
                setmealDto.setCategoryName(categoryName);

                return setmealDto;
            }
        }).collect(Collectors.toList());

        //将setmealDtoList存入setmealDtoPage中
        setmealDtoPage.setRecords(setmealDtoList);
        return R.success(setmealDtoPage);
    }

    /**
     * 删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> deleteByIds(@RequestParam List<Long> ids){
        setmealService.removeWithSetmealDish(ids);
        return R.success("删除成功");
    }


    /**
     * 查找套餐信息
     * @param categoryId
     * @param status
     * @return
     */
    @GetMapping("/list")
    public R<List<Setmeal>> getDishList(Long categoryId,Integer status) {

        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper=new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,categoryId);
        setmealLambdaQueryWrapper.eq(Setmeal::getStatus,status);
        List<Setmeal> list = setmealService.list(setmealLambdaQueryWrapper);



        return R.success(list);
    }


}
